package csoei.bbs.Dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TopicDao {
	static String sql="";
	static final String driver="com.mysql.jdbc.Driver";
	static final String url="jdbc:mysql://localhost:3306/csoeibbs";
	static final String db_username="root";
	static final String db_password="666666";
	public static ResultSet getTopics(String cid){
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		sql="select tid,title,u.username from topic t,user u where cid="+cid+"&&u.uid=t.uid order by torder asc";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			try {
				conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();
				rs=stmt.executeQuery(sql);
				return rs;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return rs;
		}
	public static ResultSet getTopicById(String tid){
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		sql="select title,u.username,content,u.uid from topic t,user u where tid="+tid+"&&u.uid=t.uid;";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			try {
				conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();
				rs=stmt.executeQuery(sql);
				return rs;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return rs;		
	}
	public static void newTopic(String cid,int uid,String title,String content){
		Connection conn=null;
		Statement stmt=null;
		sql="insert into topic (cid,uid,title,content)values("+cid+",'"+uid+"','"+title+"','"+content+"')";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			try {
				conn=DriverManager.getConnection(url, db_username, db_password);
				stmt=conn.createStatement();
				stmt.execute(sql);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}
